# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import os
import datetime
import traceback
from pymongo import MongoClient
from pymongo.helpers import DuplicateKeyError

class XsPipeline(object):
    def process_item(self, item, spider):

        chapterName = item.get('chapterName')
        chapterId = item.get('chapterId')
        bookName = item.get('bookName')
        chapterContent = "document.write('" + item.get('chapterContent').replace('\n', '').replace('\r', '').replace('\t', '') + "')"
        chapterNum = item.get('chapterNum')
        self.save_as_filename(bookName, chapterId, chapterContent)

        return item

    def save_as_file(self, chapterNum, chapterName, chapterContent):

        if not os.path.exists(r'./web/static/books/%s_%s.txt' % (chapterNum, chapterName)):
            with open(r'./web/static/books/%s_%s.txt' % (chapterNum, chapterName), 'w') as f:
                f.write(chapterContent.encode('utf-8'))

    def save_as_filename(self, bookName, chapterId, chapterContent):     
        if not os.path.exists(r'./web/static/books/%s' % bookName):
            os.makedirs(r'./web/static/books/%s' % bookName)

        if not os.path.exists(r'./web/static/books/%s/%s.txt' % (bookName, chapterId)):
            with open(r'./web/static/books/%s/%s.txt' % (bookName, chapterId), 'w') as f:
                f.write(chapterContent.encode('utf-8'))

class MongoDBPipeline(object):

    MONGODB_SERVER = "localhost"
    MONGODB_PORT = 27017
    MONGODB_DB = "QQxs"
    def __init__(self):
        try:
            client = MongoClient(self.MONGODB_SERVER, self.MONGODB_PORT)
            self.db = client[self.MONGODB_DB]
        except Exception as e:
            print 'connect db fail!\n'
            traceback.print_exc()

    def process_item(self, item, spider):
        chapter_detail = {
            'bookName': item.get('bookName'),
            'chapterId': item.get('chapterId'),
            'chapterName': item.get('chapterName'),
        }
        try:
            object_id = str(self.db['chapter_detail'].insert(chapter_detail))
        except DuplicateKeyError as e:
            print 'had!!!\n'
            object_id = e.message[e.message.find('ObjectId')+10:e.message.find('ObjectId')+10+24]

        item['chapterFileName'] = object_id


        # with open(r'./books/%s_%s.txt' % (item.get('chapterNum'), item.get('chapterName')), 'w') as f:
        #     pass

        return item
